home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15323 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: ub239.dialup.uwa.edu.au!localhost!prye
  2. From: prye@cyllene.uwa.edu.au (Peter Rye)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: another overloading question
  5. Date: 04 Apr 1996 17:12:01 GMT
  6. Organization: The University of Western Australia
  7. Distribution: world
  8. Message-ID: <PRYE.96Apr5011201@cyllene.uwa.edu.au>
  9. References: <4ju98d$3al@panoramix.fi.upm.es> <4jvst5$snq@grimsel.zurich.ibm.com>
  10. NNTP-Posting-Host: ub239.dialup.uwa.edu.au
  11. In-reply-to: wgk@zurich.ibm.com's message of 4 Apr 1996 07:14:45 GMT
  12. X-Mailer: GNU Emacs 19.28
  13.  
  14. >>>>> "Keith" == Keith Whittingham <wgk@zurich.ibm.com> writes:
  15.  
  16.     Keith> In <4ju98d$3al@panoramix.fi.upm.es>, sacha@diafi.upm.es
  17.     Keith> (Sacha) writes:
  18.  
  19.     >> Can I overload = for simple types?
  20.  
  21.     >> class Colour { 
  22.     >> private: 
  23.     >>   float r_, g_, b_;
  24.     >> public: 
  25.     >>   Colour(float r, float g, float b) {r_=r; g_=g; b_=b;}
  26.  
  27.     Keith> ++++ int operator=(void) 
  28.     Keith> ++++ { return (r_ + g_ + b_) / 3;}
  29.  
  30.     >> };
  31.     >> 
  32.     >> is there some way I can overload = so that I can do things
  33.     >> like:
  34.     >> 
  35.     >> Colour colour(0.99238, 0.172364, 0.273);
  36.     >> 
  37.     >> int intensity = colour;
  38.     >> 
  39.  
  40.     Keith> That should do the trick
  41.  
  42. I don't think so.
  43. My compiler complains:
  44.  
  45. ub239:~$ g++ -o check check.cc
  46. check.cc:11: `Colour::operator =()' must take exactly one argument
  47. check.cc:16: `class Colour' used where a `int' was expected
  48.  
  49. "Operator overloading cannot modify the rules for operators applied
  50. to types for which they are defined by the language itself." -- ARM
  51.  
  52. You perhaps meant:
  53. operator int() { return ......}
  54. --
  55. Peter Rye   prye@cyllene.uwa.edu.au,  prye@ichr.uwa.edu.au
  56. Respiratory Research Fellow, Princess Margaret Hospital for Children
  57. Perth, Western Australia    Ph: +61 (09) 340 8985, Fax: +61 (09) 388 2097
  58. ** Smoking areas in restaurants are like peeing areas in swimming pools. **
  59.